#include<bits/stdc++.h>
#define ll long long
#define F first
#define S second
#define mp make_pair
#define vi vector<int>
#define pb push_back
#define pii pair<int,int>

using namespace std;
ll sum1[262144],sum2[262144];
int ch[262144],lim[262144];bool f[262144],g[262144][2];
int A[131072],a[131072],q[131072];
ll ans[131072];char c[131072];
int n,m,k,cnt;bool rm;
bool pushup(int id,int pos,bool in)
{
    int son=id<<1|f[id];
    if(in&&!sum2[son]&&ch[son]>=lim[id]){
        sum1[id]-=sum1[son^1];
        sum2[id]-=sum2[son^1];
        g[id][f[id]^1]=0;
    }sum2[id]-=pos;
    if((!in&&(sum2[son]||ch[son]<lim[id]))||(in&&a[pos]>=lim[id]))return sum1[id]+=pos,ch[id]=a[pos],true;
    else return false;
}
void build(int l,int r,int id)
{
    g[id][0]=g[id][1]=1;
    sum1[id]=0;sum2[id]=1ll*(r-l+1)*(l+r)/2;
    if(l==r)return lim[id]=0,void();
    int mid=(l+r)>>1;
    build(l,mid,id<<1);
    build(mid+1,r,id<<1|1);
    return lim[id]=lim[id<<1]+1,void();
}
bool update(int l,int r,int pos,int id)
{
    if(l==r){
        ch[id]=a[l];
        sum1[id]=l;sum2[id]=0;
        return true;
    }int mid=(l+r)>>1;bool in,res=0;
    if(pos<=mid)res=update(l,mid,pos,id<<1),in=f[id]^1;
    else res=update(mid+1,r,pos,id<<1|1),in=f[id];
    if(res)res=pushup(id,pos,in);
    else{
        if(rm)sum2[id]-=pos;
    }
    if(!g[id][(in?f[id]:(f[id]^1))])rm=0;
    return res;
}
ll query(int l,int r,int len,int id)
{
    int mid=(l+r)>>1;
    if(mid>=len&&l!=r)return query(l,mid,len,id<<1);
    else return sum1[id]+sum2[id];
}
void solve()
{
    int x[4];
    for(int i=0;i<4;i++)scanf("%d",&x[i]);
    for(int i=1;i<=n;i++)a[i]=A[i]^x[i%4];
    build(1,k,1);
    for(int i=1;i<=n;i++)
    {
        rm=1;
        update(1,k,i,1);
        ans[i]=query(1,k,i,1);
    }ll ANS=0;
    for(int i=1;i<=m;i++)
        ANS^=ans[q[i]]*i;
    printf("%lld\n",ANS);
    return;
}
int main()
{
    freopen("arena.in","r",stdin);
    freopen("arena.out","w",stdout);
    scanf("%d%d",&n,&m);
    for(int i=1;i<=n;i++)scanf("%d",&A[i]);
    for(int i=1;i<=m;i++)scanf("%d",&q[i]);
    for(k=1;k<=n;k*=2)cnt++;int ct=k-1;
    for(int i=cnt-1;i>=0;i--){
        scanf("%s",c);int len=strlen(c);
        reverse(c,c+len);
        for(int j=0;j<len;j++)
            f[ct--]=c[j]-'0';
    }int T;scanf("%d",&T);
    while(T--)solve();
    return 0;
}